﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;

namespace MyEngineExercise.我的扩展.图层扩展类
{
    //为什么要这个类，是为了Map.AddLayers()服务的，Map.AddLayers()里面的参数一定要实现IEnumLayer
    //为什么要用Map.AddLayers(),用Map.AddLayer()不行吗，因为Map.AddLayers()会帮图层自动排序，按照点线面的顺序
    public class EnumLayerClass:IEnumLayer
    {
        /// <summary>
        /// 当前图层的索引
        /// </summary>
        private int _currentLayerIndex = -1;


        /// <summary>
        /// 所有图层的集合
        /// </summary>
        private List<ILayer> _layerList = new List<ILayer>();




        /// <summary>
        /// 图层数
        /// </summary>
        public Int32 LayerCount { get { return _layerList.Count; } }



        /// <summary>
        /// 添加图层到图层列表
        /// </summary>
        /// <param name="layer"></param>
        public void AddLayerToList(ILayer layer)
        {
            if (null != layer)
                _layerList.Add(layer);
        }



        /// <summary>
        /// 图层列表的集合
        /// </summary>
        public List<ILayer> LayerList{get{return _layerList;}}


        #region 实现IEnumLayer
        /// <summary>
        /// 下一个图层
        /// </summary>
        /// <returns></returns>
        public ILayer Next()
        {
            try
            {
                _currentLayerIndex++;

                //这里会出现异常，不要写以下语句或类似的语句进行判断，否则会导致Imap.AddLayers()方法出现异常
                //if(_layerList.Count < _currentLayerIndex +1)//如果超出索引，则返回空
                    //return null;

                return _layerList[_currentLayerIndex];
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 重新设置索引为-1
        /// </summary>
        public void Reset()
        {
            _currentLayerIndex = -1;
        }
        #endregion


        /// <summary>
        /// 构造函数
        /// </summary>
        public EnumLayerClass()
        {
        }


        /// <summary>
        /// 构造函数，添加一个图层到列表
        /// </summary>
        /// <param name="layer"></param>
        public EnumLayerClass(ILayer layer)
        {
            AddLayerToList(layer);
        }


        /// <summary>
        /// 构造函数，添加一系列图层到列表
        /// </summary>
        /// <param name="layers"></param>
        public EnumLayerClass(IEnumerable<ILayer> layers)
        {
            foreach (ILayer layer in layers)
                AddLayerToList(layer);
        }
    }
}
